package com.ruoyi.rms.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.rms.domain.MemberMessage;
import com.ruoyi.rms.service.IMemberMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员信息Controller
 * 
 * @author wyl
 * @date 2024-03-11
 */
@RestController
@RequestMapping("/rms/member")
public class MemberMessageController extends BaseController
{
    @Autowired
    private IMemberMessageService memberMessageService;

    /**
     * 查询会员信息列表
     */
//    @PreAuthorize("@ss.hasPermi('rms:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(MemberMessage memberMessage)
    {
        startPage();
        List<MemberMessage> list = memberMessageService.selectMemberMessageList(memberMessage);
        return getDataTable(list);
    }

    /**
     * 根据微信号查询会员信息
     * @param wxId
     * @return
     */
    @GetMapping("/getInfoByWxId/{wxId}")
    public AjaxResult getInfoByWxId(@PathVariable("wxId") String wxId) {
        return AjaxResult.success(memberMessageService.getInfoByWxId(wxId));
    }

    /**
     * 导出会员信息列表
     */
//    @PreAuthorize("@ss.hasPermi('rms:member:export')")
    @Log(title = "会员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MemberMessage memberMessage)
    {
        List<MemberMessage> list = memberMessageService.selectMemberMessageList(memberMessage);
        ExcelUtil<MemberMessage> util = new ExcelUtil<MemberMessage>(MemberMessage.class);
        util.exportExcel(response, list, "会员信息数据");
    }

    /**
     * 获取会员信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('rms:member:query')")
    @GetMapping(value = "/{memberId}")
    public AjaxResult getInfo(@PathVariable("memberId") Long memberId)
    {
        return success(memberMessageService.selectMemberMessageByMemberId(memberId));
    }

    /**
     * 新增会员信息
     */
//    @PreAuthorize("@ss.hasPermi('rms:member:add')")
    @Log(title = "会员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MemberMessage memberMessage)
    {
        return memberMessageService.insertMemberMessage(memberMessage) > 0 ? AjaxResult.success("操作成功") : AjaxResult.error("超过最大限额，充值失败");
    }

    /**
     * 修改会员信息
     */
//    @PreAuthorize("@ss.hasPermi('rms:member:edit')")
    @Log(title = "会员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MemberMessage memberMessage)
    {
        return memberMessageService.updateMemberMessage(memberMessage) > 0 ? AjaxResult.success("操作成功") : AjaxResult.error("超过最大限额，充值失败");
    }

    /**
     * 删除会员信息
     */
//    @PreAuthorize("@ss.hasPermi('rms:member:remove')")
    @Log(title = "会员信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{memberIds}")
    public AjaxResult remove(@PathVariable Long[] memberIds)
    {
        return toAjax(memberMessageService.deleteMemberMessageByMemberIds(memberIds));
    }

    /**
     * 充值
     * @param memberId
     * @param price
     * @return
     */
    @PutMapping("/recharge/{memberId}/{price}")
    public AjaxResult recharge(@PathVariable("memberId") Long memberId, @PathVariable("price") BigDecimal price) {
        return toAjax(memberMessageService.recharge(memberId, price));
    }
}
